<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MESI缓存一致性协议详解</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        /* 首字下沉效果 */
        .drop-cap::first-letter {
            float: left;
            font-size: 4rem;
            line-height: 3rem;
            padding-right: 0.5rem;
            margin-top: 0.5rem;
            font-weight: 700;
            color: #4f46e5;
            font-family: 'Noto Serif SC', serif;
        }
        
        /* 状态卡片悬停效果 */
        .state-card {
            transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
            backdrop-filter: blur(10px);
            background: rgba(255, 255, 255, 0.9);
        }
        
        .state-card:hover {
            transform: translateY(-8px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.15);
        }
        
        /* 渐变文字效果 */
        .gradient-text {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        
        /* 动画效果 */
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        .animate-fadeInUp {
            animation: fadeInUp 0.8s ease-out forwards;
        }
        
        /* 自定义滚动条 */
        ::-webkit-scrollbar {
            width: 10px;
        }
        
        ::-webkit-scrollbar-track {
            background: #f1f1f1;
        }
        
        ::-webkit-scrollbar-thumb {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 5px;
        }
        
        /* 标题装饰线 */
        .title-decoration {
            position: relative;
            display: inline-block;
        }
        
        .title-decoration::after {
            content: '';
            position: absolute;
            bottom: -10px;
            left: 0;
            width: 100%;
            height: 4px;
            background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        /* 列表项美化 */
        .custom-list li {
            position: relative;
            padding-left: 2rem;
            margin-bottom: 1rem;
        }
        
        .custom-list li::before {
            content: '';
            position: absolute;
            left: 0;
            top: 0.5rem;
            width: 8px;
            height: 8px;
            background: #667eea;
            border-radius: 50%;
        }
        
        /* Mermaid图表样式 */
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="relative overflow-hidden">
        <div class="absolute inset-0 bg-gradient-to-br from-purple-600 via-indigo-600 to-blue-600 opacity-90"></div>
        <div class="relative z-10 container mx-auto px-6 py-24 text-center">
            <h1 class="text-5xl md:text-7xl font-bold text-white mb-6 animate-fadeInUp">
                MESI <span class="font-light">缓存一致性协议</span>
            </h1>
            <p class="text-xl md:text-2xl text-white opacity-90 max-w-3xl mx-auto animate-fadeInUp" style="animation-delay: 0.2s">
                深入理解多核处理器系统中的数据一致性保障机制
            </p>
            <div class="mt-12 flex justify-center space-x-8 animate-fadeInUp" style="animation-delay: 0.4s">
                <div class="text-center">
                    <i class="fas fa-microchip text-4xl text-white mb-2"></i>
                    <p class="text-white">多核架构</p>
                </div>
                <div class="text-center">
                    <i class="fas fa-sync-alt text-4xl text-white mb-2"></i>
                    <p class="text-white">状态同步</p>
                </div>
                <div class="text-center">
                    <i class="fas fa-shield-alt text-4xl text-white mb-2"></i>
                    <p class="text-white">数据一致性</p>
                </div>
            </div>
        </div>
        <div class="absolute bottom-0 left-0 right-0">
            <svg viewBox="0 0 1440 120" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path d="M0 120L60 110C120 100 240 80 360 70C480 60 600 60 720 65C840 70 960 80 1080 85C1200 90 1320 90 1380 90L1440 90V120H1380C1320 120 1200 120 1080 120C960 120 840 120 720 120C600 120 480 120 360 120C240 120 120 120 60 120H0V120Z" fill="white"/>
            </svg>
        </div>
    </section>

    <!-- 主要内容区 -->
    <main class="container mx-auto px-6 py-16">
        <!-- 引言部分 -->
        <section class="max-w-4xl mx-auto mb-16">
            <div class="bg-white rounded-2xl shadow-xl p-8 md:p-12">
                <p class="text-lg leading-relaxed text-gray-700 drop-cap">
                    MESI（Modified, Exclusive, Shared, Invalid）缓存一致性协议是一种广泛应用于多核处理器系统中的缓存一致性协议，用于确保多个处理器核心在访问共享内存时，缓存中的数据保持一致。MESI协议通过四种状态来管理缓存中的数据，并定义了处理器之间如何进行通信以确保数据一致性。
                </p>
            </div>
        </section>

        <!-- MESI四种状态 -->
        <section class="mb-20">
            <h2 class="text-4xl font-bold text-center mb-12">
                <span class="title-decoration gradient-text">MESI协议的四种状态</span>
            </h2>
            
            <div class="grid grid-cols-1 md:grid-cols-2 gap-8 max-w-6xl mx-auto">
                <!-- Modified状态 -->
                <div class="state-card rounded-2xl shadow-lg p-8 border-t-4 border-red-500">
                    <div class="flex items-center mb-4">
                        <div class="w-16 h-16 bg-red-100 rounded-full flex items-center justify-center mr-4">
                            <i class="fas fa-edit text-2xl text-red-600"></i>
                        </div>
                        <h3 class="text-2xl font-bold text-gray-800">Modified <span class="text-gray-500 font-normal">(M, 修改态)</span></h3>
                    </div>
                    <ul class="custom-list text-gray-700">
                        <li>数据在当前缓存行中被修改，与主存中的数据不一致。</li>
                        <li>数据仅存在于当前处理器的缓存中，其他处理器缓存中没有该数据。</li>
                        <li>在写回主存之前，其他处理器若请求该数据，当前处理器需先将数据写回主存再将其状态设置为共享态（S）或无效态（I）。</li>
                    </ul>
                </div>

                <!-- Exclusive状态 -->
                <div class="state-card rounded-2xl shadow-lg p-8 border-t-4 border-yellow-500">
                    <div class="flex items-center mb-4">
                        <div class="w-16 h-16 bg-yellow-100 rounded-full flex items-center justify-center mr-4">
                            <i class="fas fa-lock text-2xl text-yellow-600"></i>
                        </div>
                        <h3 class="text-2xl font-bold text-gray-800">Exclusive <span class="text-gray-500 font-normal">(E, 独占态)</span></h3>
                    </div>
                    <ul class="custom-list text-gray-700">
                        <li>数据未被修改，且与主存中的数据一致。</li>
                        <li>数据仅存在于当前处理器的缓存中，其他处理器缓存中没有该数据。</li>
                        <li>当前处理器可以自由读取或写入该数据而无需与其他处理器通信，若发生写操作，状态会转变为修改态（M）。</li>
                    </ul>
                </div>

                <!-- Shared状态 -->
                <div class="state-card rounded-2xl shadow-lg p-8 border-t-4 border-green-500">
                    <div class="flex items-center mb-4">
                        <div class="w-16 h-16 bg-green-100 rounded-full flex items-center justify-center mr-4">
                            <i class="fas fa-share-alt text-2xl text-green-600"></i>
                        </div>
                        <h3 class="text-2xl font-bold text-gray-800">Shared <span class="text-gray-500 font-normal">(S, 共享态)</span></h3>
                    </div>
                    <ul class="custom-list text-gray-700">
                        <li>数据未被修改，与主存中的数据一致。</li>
                        <li>数据可以存在于多个处理器的缓存中，多个处理器可以共享该数据的副本。</li>
                        <li>任何处理器都可以读取该数据，但不能写入，若发生写操作，则需先将该数据的其他副本设为无效态（I），然后状态转换为修改态（M）。</li>
                    </ul>
                </div>

                <!-- Invalid状态 -->
                <div class="state-card rounded-2xl shadow-lg p-8 border-t-4 border-gray-500">
                    <div class="flex items-center mb-4">
                        <div class="w-16 h-16 bg-gray-100 rounded-full flex items-center justify-center mr-4">
                            <i class="fas fa-ban text-2xl text-gray-600"></i>
                        </div>
                        <h3 class="text-2xl font-bold text-gray-800">Invalid <span class="text-gray-500 font-normal">(I, 无效态)</span></h3>
                    </div>
                    <ul class="custom-list text-gray-700">
                        <li>当前缓存行中的数据无效，处理器不能使用该数据。</li>
                        <li>其他处理器的缓存可能拥有该数据的有效副本。</li>
                    </ul>
                </div>
            </div>
        </section>

        <!-- 状态转换图 -->
        <section class="mb-20">
            <h2 class="text-4xl font-bold text-center mb-12">
                <span class="title-decoration gradient-text">MESI状态转换图</span>
            </h2>
            <div class="bg-white rounded-2xl shadow-xl p-8">
                <div class="mermaid">
                    graph TD
                        I[Invalid 无效态] 
                        E[Exclusive 独占态]
                        S[Shared 共享态]
                        M[Modified 修改态]
                        
                        I -->|PrRd/BusRd<br/>独占读取| E
                        I -->|PrRd/BusRd<br/>共享读取| S
                        I -->|PrWr/BusRdX| M
                        
                        E -->|PrWr| M
                        E -->|BusRd| S
                        E -->|BusRdX| I
                        
                        S -->|PrWr/BusUpgr| M
                        S -->|BusRdX| I
                        
                        M -->|BusRd/Flush| S
                        M -->|BusRdX/Flush| I
                        
                        style I fill:#f9f9f9,stroke:#666,stroke-width:2px
                        style E fill:#fff4e6,stroke:#f59e0b,stroke-width:2px
                        style S fill:#e6f7e6,stroke:#10b981,stroke-width:2px
                        style M fill:#fee2e2,stroke:#ef4444,stroke-width:2px
                </div>
            </div>
        </section>

        <!-- 操作与状态转换 -->
        <section class="mb-20">
            <h2 class="text-4xl font-bold text-center mb-12">
                <span class="title-decoration gradient-text">操作与状态转换</span>
            </h2>
            